﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Data.OleDb;
using Models;
using System.Web.Security;

namespace Lohus.Controllers
{
    public class AboutUsController : Controller
    {
        //
        // GET: /AboutUs/

        public ActionResult Index()
        {

            if (FormsAuthentication.GetAuthCookie("user", false) == null)
            {
                throw new Exception("无访问权限！");
            }

            string sql = "select top 1 id,content from aboutus order by id desc";
            using (OleDbConnection con = new OleDbConnection(DBHelper.DBHelper.connectionString))
            {
                try
                {
                    con.Open();
                    OleDbCommand cmd = new OleDbCommand(sql, con);
                    using (OleDbDataReader dr = cmd.ExecuteReader())
                    {
                        if (dr != null)
                        {
                            while (dr.Read())
                            {
                                ViewData["id"] = dr["id"].ToString();
                                ViewData["content"] = dr["content"].ToString();

                            }
                        }
                    }
                }
                catch
                {
                    ViewData["id"] = "";
                    ViewData["content"] = "";
                }
            }

            return View();
        }
        /// <summary>
        /// 获取文章
        /// </summary>
        /// <returns></returns>
        public ActionResult GetAboutUsArticle()
        {
            string sql = "select top 1 id,content from aboutus order by id desc";
            using (OleDbConnection con = new OleDbConnection(DBHelper.DBHelper.connectionString))
            {
                try
                {
                    con.Open();
                    OleDbCommand cmd = new OleDbCommand(sql, con);
                    using (OleDbDataReader dr = cmd.ExecuteReader())
                    {
                        if (dr != null)
                        {
                            while (dr.Read())
                            {
                                AboutUs aboutus = new AboutUs() { ID = Convert.ToInt32(dr["id"].ToString()), Content = dr["content"].ToString() };
                                return Json(aboutus, JsonRequestBehavior.AllowGet);
                            }
                        }
                    }
                }
                catch
                {
                }
            }
            AboutUs aboutuss = new AboutUs();
            return Json(aboutuss, JsonRequestBehavior.AllowGet);

        }
        /// <summary>
        /// 保存文章
        /// </summary>
        /// <param name="id"></param>
        /// <param name="content"></param>
        /// <returns></returns>
        [ValidateInput(false)]
        public ActionResult SaveArticle(string ID, string Content)
        {
            if (FormsAuthentication.GetAuthCookie("user", false) == null)
            {
                throw new Exception("无访问权限！");
            }

            if (string.IsNullOrEmpty(Content))
            {
                Content = Content.Trim('\'');
            }
            string sql = "";
            if (string.IsNullOrEmpty(ID))
            {
                ID = "1";
                sql = "insert into aboutus(content,id) values('" + Content + "'," + ID + ")";
            }
            else
            {
                sql = "update aboutus set content  = '" + Content + "' where id =" + ID;
            }
            using (OleDbConnection con = new OleDbConnection(DBHelper.DBHelper.connectionString))
            {
                try
                {
                    con.Open();
                    OleDbCommand cmd = new OleDbCommand(sql, con);
                    return Json(cmd.ExecuteNonQuery(), JsonRequestBehavior.AllowGet);
                }
                catch
                {
                    return Json(0, JsonRequestBehavior.AllowGet);
                }
            }
        }

        /// <summary>
        /// 编辑关于罗豪斯文章
        /// </summary>
        /// <returns></returns>
        public ActionResult EditArticle()
        {
            return View();
        }
    }
}
